
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Model.World;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;


/**
 *
 * @author Daniel
 */
public class View {

    private int BOXEL_WIDTH, BOXEL_HEIGHT,
            WIDTH, HEIGHT; //Of the window!
    Dimension _screen; //Of the Computer screen
    Point _wndw_loc; //Where in the screen do we initialize the window?  
    Graphics2D _gAdmin; //Responsible of painting the boxels -- TODO?: Maybe threading it, one Graphics2D per boxel?
     

    public View(int BH, int BW, int NBH, int NBW, Point place) {
        BOXEL_WIDTH = BW;
        BOXEL_HEIGHT = BH;
        WIDTH = (NBW * BOXEL_WIDTH);
        HEIGHT = (NBH * BOXEL_HEIGHT);
        
        if (place == null) { //If no position is specified, the window is screen-centered by default
            _screen = Toolkit.getDefaultToolkit().getScreenSize();
            _wndw_loc = new Point(((int) _screen.getWidth() - WIDTH) / 2, ((int) _screen.getHeight() - HEIGHT) / 2);
        } else {
            _wndw_loc = new Point((int) place.getX(), (int) place.getY());
        }
       
    }

    public void paint(World map, Point cameraCentre) { //TODO: de cameraCentre dependrà     
        for (int i = 2; i < HEIGHT; i = i + BOXEL_HEIGHT) {
            for (int j = 0; j < WIDTH; j = j + BOXEL_WIDTH) {
                try {                    
                   
                    _gAdmin.drawImage(map.get_Boxel(j/BOXEL_WIDTH, i/BOXEL_HEIGHT), j, i+BOXEL_HEIGHT, null);
                } catch (Exception e) {
                    System.err.append("Haven't drawn at (" + j + ", " + i + ");\n");
                }

            }
        }
    }

    public void init() {

        //Special testing frame adapted with the View's attributes.
        //TODO?: Where do I declare it? Where do I init it?
        //Perhaps I should downcast it from here, specified by input constructor type_spec.
        EFrame p = new EFrame(WIDTH, 2 /*Aesthetic -- massa proper al sostre del frame */
                + HEIGHT + 1* BOXEL_HEIGHT /*Per alguna raó 
         * se salta la ultima linia de Boxels, INVESTIGATE
                 
                 * Causa: Graphics 2D comença a dibuixar com en un eix de coordenades
                 matematicament estàndard, mentres que les coordenades del Frame
                 són informàticament estàndard
                 
                 MATH           COMPUTERS
                 ^                  x
                 |            y +--->
                y|              |
                 +--->          |
                 * x            v
                 
                 Per tant comença a dibuixar amb un Boxel de diferència.
                 */
                , BOXEL_WIDTH, BOXEL_HEIGHT, _wndw_loc, "COOL GAME");
        p.start();

        //Basically, the graphics administrator knows all of the Frame/Panel's attributes and
        //can modify them to will - including paintingon them.
        _gAdmin = (Graphics2D) p.getGraphics();
        
    }
}
